package com.wxd.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wxd.reggie.common.R;
import com.wxd.reggie.dto.SetmealDto;
import com.wxd.reggie.entity.Category;
import com.wxd.reggie.entity.Setmeal;
import com.wxd.reggie.service.CategoryService;
import com.wxd.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 添加套餐
     *
     * @param setmealDto
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        log.info("参数{}", setmealDto);
        setmealService.saveMeal(setmealDto);
        return R.success("添加成功");
    }

    /**
     * 获取套餐列表
     *
     * @param page
     * @param pageSize
     * @param name
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        Page<SetmealDto> info = new Page();
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.like(name != null, Setmeal::getName, name);
        lqw.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(pageInfo, lqw);
        // 进行拷贝
        BeanUtils.copyProperties(pageInfo, info, "records");
        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> list = records.stream().map(item -> {
            SetmealDto mealDto = new SetmealDto();
            BeanUtils.copyProperties(item, mealDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if (category != null) {
                mealDto.setCategoryName(category.getName());
            }
            return mealDto;
        }).collect(Collectors.toList());
        info.setRecords(list);
        return R.success(info);
    }

    /**
     * 删除套餐
     *
     * @param ids
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids) {
        log.info("ids数据{}", ids);
        setmealService.removeMealWithDish(ids);
        return R.success("删除成功");
    }


    @PostMapping("/status/{status}")
    public R<String> status(@PathVariable int status, String ids) {
        if (ids == null || ids.isEmpty()) {
            return R.error("ids列表不能为空");
        }
        String[] idArray = ids.split(",");
        List<Long> idList = Arrays.stream(idArray).map(Long::valueOf).collect(Collectors.toList());
        LambdaUpdateWrapper<Setmeal> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Setmeal::getStatus, status).in(Setmeal::getId, idList);
        boolean update = setmealService.update(updateWrapper);
        log.info("更新的结果{}", update);
        return R.success("操作成功");
    }

    /**
     * 根据分类id查询分类数据
     *
     * @param setmeal
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal) {
        Long categoryId = setmeal.getCategoryId();
        Integer status = setmeal.getStatus();
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(categoryId != null, Setmeal::getCategoryId, categoryId).eq(status != null, Setmeal::getStatus, status);
        List<Setmeal> list = setmealService.list(lqw);
        return R.success(list);
    }
}
